#!/bin/sh

function desktop_sort
{
cat "$1" | awk '
BEGIN {idx = 0; FS="\\[|\\]"}
NR==2 {idx = 10}
{idx += 1}
/^Name=/ {print "2aa\t" $0; next}
/^Name\[.*\]/ {print "2" $2 "\t" $0; next}
/^Comment=/ {print "3aa\t" $0; next}
/^Comment\[.*\]/ {print "3" $2 "\t" $0; next}
/^GenericName=/ {print "4aa\t" $0; next}
/^GenericName\[.*\]/ {print "4" $2 "\t" $0; next}
/^URL=/ {print "5aa\t" $0; next}
/^URL\[.*\]/ {print "5" $2 "\t" $0; next}
/^Description=/ {print "6aa\t" $0; next}
/^Description\[.*\]/ {print "6" $2 "\t" $0; next}
{print idx "\t" $0}' | sort -n | cut -f 2, > "$1.tmp"
mv -f "$1.tmp" "$1"
}

# Error fixes
sed -i -s '3 iName=Desktop' root/Desktop/.directory
sed -i -s '3 iName=Desktop' etc/skel/Desktop/.directory
sed -i -s  '/^Comment\[pl\]=/ {d;Q}' root/Desktop/trash.desktop
sed -i -s  '/^Comment\[pl\]=/ {d;Q}' etc/skel/Desktop/trash.desktop
sed -i -s '3 iName=Documents' root/Documents/.directory
sed -i -s '3 iName=Documents' etc/skel/Documents/.directory
sed -i -s '3 iName=Images' root/Pictures/.directory
sed -i -s '3 iName=Music' root/Music/.directory
sed -i -s  '/^Name=Music/ {d;Q}' etc/skel/Music/.directory
sed -i -s '3 iName=Music' etc/skel/Music/.directory
sed -i -s 's/^GenericName=$/GenericName=My Computer/g' root/Desktop/My\ Computer.desktop
sed -i -s 's/^GenericName=$/GenericName=My Computer/g' etc/skel/Desktop/My\ Computer.desktop
sed -i -s 's/^GenericName=$/GenericName=Install PCLinuxOS/g;s/Name\[pl\]=Instalacja PCLinuxOS//' root/Desktop/Install\ PCLinuxOS.desktop
sed -i -s 's/^GenericName=$/GenericName=Install PCLinuxOS/g;s/Name\[pl\]=Instalacja PCLinuxOS//' etc/skel/Desktop/Install\ PCLinuxOS.desktop
sed -i -s 's/^Comment=$/Comment=Install PCLinuxOS on your hard drive/g' root/Desktop/Install\ PCLinuxOS.desktop
sed -i -s 's/^Comment=$/Comment=Install PCLinuxOS on your hard drive/g' etc/skel/Desktop/Install\ PCLinuxOS.desktop

echo >> etc/skel/Music/.directory
echo >> root/Documents/.directory
echo >> etc/skel/Documents/.directory
echo >> etc/skel/Pictures/.directory

sed -i -s 's/^Comment=$/Comment=VND image/g' usr/share/mimelnk/image/vnd.desktop
sed -i -s '3 iComment=SVG + XML image' usr/share/mimelnk/image/svg-xml.desktop
sed -i -s '3 iComment=WMF image' usr/share/mimelnk/image/wmf.desktop
sed -i -s '3 iComment=JPG image' usr/share/mimelnk/image/jpg.desktop

mv -f root/Desktop/Installation\ Help root/Desktop/Installation\ Help.desktop
mv -f etc/skel/Desktop/Installation\ Help etc/skel/Desktop/Installation\ Help.desktop

mv -f root/Desktop/New\ User\ Guide root/Desktop/New\ User\ Guide.desktop
mv -f etc/skel/Desktop/New\ User\ Guide etc/skel/Desktop/New\ User\ Guide.desktop

cat > de.tmp <<EOD
[Desktop Entry]
Encoding=UTF-8
Type=MimeType
MimeType=application/x-applixspread
Icon=spreadsheet
Patterns=*.as
Comment=Applix Spreadsheets Document
EOD

cat de.tmp usr/share/mimelnk/application/x-applix-spread.desktop > de2.tmp
mv -f de2.tmp usr/share/mimelnk/application/x-applix-spread.desktop
rm -f de.tmp

cat > de.tmp <<EOD2
[Desktop Entry]
Type=MimeType
Comment=X window image
MimeType=image/x-xwindowdump
Patterns=*.xwd;
EOD2

cat de.tmp usr/share/mimelnk/image/x-windowdump.desktop > de2.tmp
mv -f de2.tmp usr/share/mimelnk/image/x-windowdump.desktop
rm -f de.tmp

rm -f `ls usr/share/mimelnk/message/*.desktop | grep -v "x-gnu-rmail.desktop"`

find . -name *.desktop -print | (while read; do desktop_sort "${REPLY}"; done )

# intltool adaption process
mkdir po
intltool-prepare
cd po
sed -i -e '1 i[encoding: UTF-8]' POTFILES.in
INTLTOOL_EXTRACT=/usr/bin/intltool-extract intltool-update -p -g desktop-files

cat > header <<EOT
# Translation of PCLinuxOS desktop files to %locale.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PCLinuxOS 2007\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2008-04-23 16:03+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <%locale@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

EOT

for fil in `ls *.po`
do
    LOCALE=${fil%%.po}
    echo "Processing $LOCALE ..."
    sed -e "s/%locale/$LOCALE/g" header > header.tmp
    cat header.tmp "$fil" > "$fil.tmp"
    mv -f "$fil.tmp" "$fil"
    INTLTOOL_EXTRACT=/usr/bin/intltool-extract intltool-update -d -g desktop-files $LOCALE
done
rm -f header.tmp

cd ..

find . -name *.in -not -name POTFILES.in -print | (while read; do rm -f "${REPLY%%.in}"; done )
